/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2024-10-10
 * Time: 12:50
 */
public class MydoubleLinkedlist implements IList{
    static class listnode{
        public int val;
        public listnode pre;
        public listnode next;

        public listnode(int val) {
            this.val = val;
        }
    }
    public listnode head;
    public listnode last;

    public boolean isempty(){
        if(head==null){
            return true;
        }else{
            return false;
        }
    }
    @Override
    public void addFirst(int data) {
        listnode p=new listnode(data);
        if(isempty()){
            head=last=p;
        }else{
            head.pre=p;
            p.next=head;
            head=p;
        }
    }

    @Override
    public void addLast(int data) {
        listnode p=new listnode(data);
        if(isempty()){
            head=p;
        }else{
            last.next=p;
            p.pre=last;
            last=p;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        listnode p=new listnode(data);
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        //找到下标为index的节点
        listnode pcur=head;
        for (int i = 0; i < index; i++) {
            pcur= pcur.next;
        }
        p.next=pcur;
        p.pre=pcur.pre;
        pcur.pre.next=p;
        pcur.pre=p;
    }

    @Override
    public boolean contains(int key) {
        listnode pcur=head;
        while(pcur!=null){
            if(pcur.val==key){
                return true;
            }
            pcur=pcur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        listnode pcur=head;
        if(pcur==null) return;
        if(pcur.val==key){
            head=pcur.next;
            pcur=null;
            return;
        }
        while(pcur.next!=null){
            if(pcur.val==key){
                pcur.pre.next=pcur.next;
                pcur.next.pre=pcur.pre;
                pcur.pre=pcur.next=null;
                return;
            }
            pcur=pcur.next;
        }
        if(pcur.val==key){
            last=last.pre;
            last.next=null;
            pcur.pre=null;
        }
    }

    @Override
    public void removeAllKey(int key) {
        listnode pcur=head;
        if(pcur==null) return;
        while(pcur.val==key){
            head=pcur.next;
            pcur=head;
        }
        while(pcur.next!=null){
            if(pcur.val==key){
                pcur.pre.next=pcur.next;
                pcur.next.pre=pcur.pre;
            }
            pcur=pcur.next;
        }
        if(pcur.val==key){
            last=last.pre;
            last.next=null;
            pcur.pre=null;
        }
    }

    @Override
    public int size() {
        listnode pcur=head;
        int count=0;
        while(pcur!=null){
            pcur=pcur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        while(head!=null){
            listnode pcur=head;
            head=head.next;
            pcur=null;
        }
    }

    @Override
    public void display() {
        listnode pcur=head;
        while(pcur!=null){
            System.out.print(pcur.val+" ");
            pcur=pcur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        MydoubleLinkedlist l=new MydoubleLinkedlist();
        //头插
        l.addFirst(1);
        l.addFirst(2);
        l.addFirst(3);
        l.addFirst(4);
        //尾插
        l.addLast(5);
        l.addLast(5);
        l.addLast(5);
        //指定位置插入
        l.addIndex(3,9);
        l.addIndex(0,10);
        l.addIndex(l.size(), 33);
        System.out.println("插入数据后：");
        l.display();

        System.out.println("是否包含5："+l.contains(5));
        System.out.println("是否包含32："+l.contains(32));

        //删除出现的所有节点
        System.out.print("删除所有的5后：");
        l.removeAllKey(5);
        l.display();
        //清空链表
        System.out.println("清空链表后：");
        l.clear();
        l.display();
    }
}
